package com.gtgj.widget.util;

import java.io.*;
import java.math.BigDecimal;
import java.util.*;
import java.util.Map.Entry;
import java.util.zip.GZIPInputStream;

public class TypeUtils {
    private static final String TAG = "GTGJ_Converter";
    private static final int INT_CONVERT_ERROR = -1;
    private static final double DOUBLE_CONVERT_ERROR = -1d;
    private static final float FLOAT_CONVERT_ERROR = -1.0f;
    private static final long LONG_CONVERT_ERROR = -1l;
    private static final int BUFFER_SIZE = 1024;

    /*
     * String to int
     */
    public static int StringToInt(final String str) {
        if (TextUtils.isEmpty(str)) {
            return INT_CONVERT_ERROR;
        }
        if (!str.matches("^-?[0-9]\\d*$")) {
            return INT_CONVERT_ERROR;
        }
        try {
            return Integer.parseInt(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return INT_CONVERT_ERROR;
    }

    /*
     * object to int
     */
    public static int ObjToInt(final Object obj) {
        if (obj != null) {
            try {
                return Integer.parseInt(String.valueOf(obj));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return INT_CONVERT_ERROR;
    }

    /*
     * object to int
     */
    public static float ObjToFloat(final Object obj) {
        if (obj != null) {
            try {
                return Float.parseFloat(String.valueOf(obj));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return FLOAT_CONVERT_ERROR;
    }

    /*
     * string to double
     */
    public static double StringToDouble(final String str) {
        if (TextUtils.isEmpty(str)) {
            return DOUBLE_CONVERT_ERROR;
        }
        try {
            return Double.parseDouble(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DOUBLE_CONVERT_ERROR;
    }

    public static boolean StringToBool(final String str) {
        if (TextUtils.isEmpty(str)) {
            return false;
        }

        return "1".equals(str) || "true".equalsIgnoreCase(str);
    }

    /*
     * string to float
     */
    public static float StringToFloat(final String str) {
        if (TextUtils.isEmpty(str)) {
            return FLOAT_CONVERT_ERROR;
        }
        try {
            return Float.parseFloat(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return FLOAT_CONVERT_ERROR;
    }

    public static float StringToFloat(final String str, float def) {
        if (TextUtils.isEmpty(str)) {
            return def;
        }
        try {
            return Float.parseFloat(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return def;
    }

    /*
     * string to long
     */
    public static long StringToLong(final String str) {
        if (!TextUtils.isEmpty(str)) {
            try {
                return Long.parseLong(str);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return LONG_CONVERT_ERROR;
    }

    /*
     * string to long
     */
    public static long StringToLong(final String str, long def) {
        if (TextUtils.isEmpty(str)) {
            return def;
        }
        try {
            return Long.parseLong(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return def;
    }

    /*
     * obj to long
     */
    public static long ObjToLong(final Object obj) {
        if (obj != null) {
            try {
                return Long.parseLong(String.valueOf(obj));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return INT_CONVERT_ERROR;
    }

    /*
     * get string from Map<String, Object>
     */
    public static String StrFromObjMap(final Map<String, Object> map, String key) {
        if (map == null || !map.containsKey(key)) {
            return "";
        }
        Object obj = map.get(key);
        if (obj == null) {
            return "";
        }
        return String.valueOf(obj);
    }

    /*
     * get string from Map<String, Object>
     */
    public static String StrFromStrMap(final Map<String, String> map, String key) {
        if (map == null || !map.containsKey(key)) {
            return "";
        }
        String obj = map.get(key);
        if (obj == null) {
            return "";
        }
        return obj;
    }

    /*
     * get int from Map<String, Object>
     */
    public static int IntFromObjMap(final Map<String, Object> map, String key) {
        if (map == null || !map.containsKey(key)) {
            return -1;
        }
        Object obj = map.get(key);
        if (obj == null) {
            return -1;
        }
        return StringToInt(String.valueOf(obj));
    }

    /*
     * get boolean from Map<String, Object>
     */
    public static boolean booleanFromObjMap(final Map<String, Object> map, String key, boolean def) {
        if (map == null || !map.containsKey(key)) {
            return def;
        }
        Object obj = map.get(key);
        if (obj == null) {
            return def;
        }

        if (obj instanceof Boolean) {
            return (Boolean) obj;
        }

        if (obj instanceof String) {
            return "1".equals(String.valueOf(obj));
        }

        return false;
    }

    /*
     * get boolean from Map<String, Object>
     */
    public static boolean booleanFromObjMap(final Map<?, ?> map, String key) {
        if (map == null || !map.containsKey(key)) {
            return false;
        }
        Object obj = map.get(key);
        if (obj == null) {
            return false;
        }

        if (obj instanceof Boolean) {
            return (Boolean) obj;
        }

        if (obj instanceof String) {
            return "1".equals(String.valueOf(obj));
        }

        return false;
    }

    /*
     * get Map<String, Object> from Map<String, Object>
     */
    public static Map<String, Object> MapFromObjMap(final Map<String, Object> map, String key) {
        if (map == null || !map.containsKey(key)) {
            return null;
        }

        Object obj = map.get(key);
        if (obj == null || !(obj instanceof Map)) {
            return null;
        }

        return (Map<String, Object>) obj;
    }

    /*
     * get Map<String, String> from Map<String, Object>
     */
    public static Map<String, String> strMapFromObjMap(final Map<String, Object> map, String key) {
        if (map == null || !map.containsKey(key)) {
            return null;
        }

        Object obj = map.get(key);
        if (obj == null || !(obj instanceof Map)) {
            return null;
        }

        return (Map<String, String>) obj;
    }

    /*
     * 获取指定类型
     */
    public static <T> T fromObjMap(final Map<String, Object> map, String key) {
        if (map == null || !map.containsKey(key)) {
            return null;
        }

        Object obj = map.get(key);
        if (obj == null) {
            return null;
        }

        return (T) obj;
    }

    /*
     * get List<Map<String, Object> from Map<String, Object>
     */
    public static List<Map<String, Object>> MapArrayFromObjMap(final Map<String, Object> map, String key) {
        if (map == null || !map.containsKey(key)) {
            return null;
        }

        Object obj = map.get(key);
        if (obj == null || !(obj instanceof List)) {
            return null;
        }

        return (List<Map<String, Object>>) obj;
    }

    public static <T> List<T> ArrayFromObjMap(final Map<String, Object> map, String key) {
        if (map == null || !map.containsKey(key)) {
            return null;
        }

        Object obj = map.get(key);
        if (obj == null || !(obj instanceof List)) {
            return null;
        }

        return (List<T>) obj;
    }

    /*
     * get int from Map<String, Object>
     */
    public static double doubleFromObjMap(final Map<String, Object> map, String key) {
        if (map == null || !map.containsKey(key)) {
            return DOUBLE_CONVERT_ERROR;
        }
        Object obj = map.get(key);
        if (obj == null) {
            return DOUBLE_CONVERT_ERROR;
        }
        return StringToDouble(String.valueOf(obj));
    }

    public static float floatFromObjMap(final Map<String, Object> map, String key) {
        if (map == null || !map.containsKey(key)) {
            return FLOAT_CONVERT_ERROR;
        }
        Object obj = map.get(key);
        if (obj == null) {
            return FLOAT_CONVERT_ERROR;
        }
        return StringToFloat(String.valueOf(obj));
    }

    public static String convertInputStreamToString(InputStream in) {
        if (in == null) {
            return "";
        }

        String result = "";
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        byte buffer[] = new byte[BUFFER_SIZE];
        int len;
        try {
            while ((len = in.read(buffer)) != -1) {
                os.write(buffer, 0, len);
            }
            result = new String(os.toByteArray(), "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                os.close();
            } catch (IOException e) {
            }
        }
        return result;
    }

    public static InputStream decompress(byte[] bs) {
        if (bs == null || bs.length == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(bs);
        try {
            GZIPInputStream gunzip = new GZIPInputStream(in);
            byte[] buffer = new byte[256];
            int n;
            while ((n = gunzip.read(buffer)) >= 0) {
                out.write(buffer, 0, n);
            }
        } catch (IOException e) {
        }
        try {
            return new ByteArrayInputStream(out.toByteArray());
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static byte[] convertInputStreamToBytes(InputStream in) {
        byte[] result = null;
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        byte buffer[] = new byte[1024];
        int len = -1;
        try {
            while ((len = in.read(buffer)) != -1) {
                os.write(buffer, 0, len);
            }
            result = os.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                os.close();
            } catch (IOException e) {
            }
        }
        return result;
    }

    /*
     * String to int
     */
    public static int StringToInt(final String str, int defaultValue) {
        if (TextUtils.isEmpty(str)) {
            return defaultValue;
        }
        try {
            return Integer.parseInt(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return defaultValue;
    }

    /*
     * 讲序列化对象转成字节流
     */
    public static byte[] serializableToBytes(Serializable object) {
        ByteArrayOutputStream baos = null;
        ObjectOutputStream oos = null;
        try {
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(object);
            return baos.toByteArray();

        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            try {
                if (baos != null) {
                    baos.close();
                }
                if (oos != null) {
                    oos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /*
     * 讲字节流转转为对象
     */
    public static Object bytesToserializable(byte[] bytes) {
        ByteArrayInputStream bais = null;
        ObjectInputStream ois = null;
        try {
            bais = new ByteArrayInputStream(bytes);
            ois = new ObjectInputStream(bais);
            return ois.readObject();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bais != null) {
                    bais.close();
                }
                if (ois != null) {
                    ois.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /*
     * 将数组转换成集合数组
     */
    public static <T> List<T> arrayToArrayList(T[] array) {
        if (array == null) {
            return null;
        }

        if (array.length <= 0) {
            return new ArrayList<T>();
        }

        List<T> arrayList = new ArrayList<T>();
        for (T t : array) {
            arrayList.add(t);
        }

        return arrayList;
    }

    /*
     * 从Map中获取Object
     */
    public static Object objFromMap(Map<String, Object> map, String key) {
        if (map == null || !map.containsKey(key)) {
            return null;
        }

        return map.get(key);
    }

    /*
     * 获取list
     */
    public static List<Object> listFromMap(Map<String, Object> map, String key) {
        if (map == null || !map.containsKey(key)) {
            return null;
        }

        Object obj = map.get(key);
        if (obj == null || !(obj instanceof List)) {
            return null;
        }

        return (List<Object>) obj;
    }

    /*
     * map转成集合
     */
    public static <T> List<T> listToMap(Map<String, T> map) {
        List<T> list = new ArrayList<T>();
        if (map == null || map.size() <= 0) {
            return list;
        }

        for (Entry<String, T> entry : map.entrySet()) {
            list.add(entry.getValue());
        }

        return list;
    }

    public static boolean containsIgnoreCase(String src, String p) {
        if (src == null || p == null) {
            return false;
        }
        String upper_src = src.toUpperCase();
        String upper_p = p.toUpperCase();
        return upper_src.contains(upper_p);
    }

    /*
     * 保留2位小数
     */
    public static double FormatDoubleRound(double source) {
        try {
            // 保留两位小数
            BigDecimal decimal = new BigDecimal(source);
            return decimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        } catch (Exception e) {
            return source;
        }
    }

    public static void converMapObjectToMapString(Map<String, Object> source, Map<String, String> dest) {
        if (source == null) {
            return;
        }

        if (dest == null) {
            dest = new HashMap<String, String>();
        }

        Set<Entry<String, Object>> datas = source.entrySet();
        if (null != datas) {
            for (Entry<String, Object> entry : datas) {
                if (entry.getValue() instanceof String) {
                    dest.put(entry.getKey(), (String) entry.getValue());
                }
            }
        }
    }

    public static void converMapStringToMapObject(Map<String, String> source, Map<String, Object> dest) {
        if (source == null) {
            return;
        }

        if (dest == null) {
            dest = new HashMap<String, Object>();
        }

        Set<Entry<String, String>> datas = source.entrySet();
        if (null != datas) {
            for (Entry<String, String> entry : datas) {
                dest.put(entry.getKey(), entry.getValue());
            }
        }
    }

    public String objectToString(Object obj) {
        if (obj == null) {
            return "";
        }
        if (obj instanceof char[]) {
            return new String((char[]) obj);
        }
        try {
            return String.valueOf(obj);
        } catch (Exception ex) {

        }
        return "";
    }
}
